#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@File ：CartController.py
@Author ：zqy
@Email : zqingy@work@163.com 
@note: 购物车
"""
from flask import g
from flask_restx import Namespace, Resource

from application.Parser.CartParser import CartValidator
from application.controller import register_crud_model
from application.logic.CartLogic import CartLogic
from application.util.ResponseUtil import ResponseUtil
from application.enumeration.StatusCodeEnum import StatusCodeEnum
from application.exception.BasicException import BasicException

cart_ns = Namespace('cart', description='购物车模块')
cart_parser: CartValidator = CartValidator
register_crud_model(
    ns=cart_ns,
    logic_cls=CartLogic,
    parser=cart_parser,
    route="",
    id_type="int",
    include_operations=["get", "list", "put", "post"]
)

@cart_ns.route("/<int:_id>")
class CartController(Resource):
    def delete(self, _id):
        """清空购物车"""
        c = CartLogic.model.get_by_id(_id)
        if not c:
            raise BasicException(status_code=StatusCodeEnum.ERROR.value, error_message="购物车不存在")
        if c.user_id != g.user.id:
            raise BasicException(status_code=StatusCodeEnum.ERROR.value, error_message="购物车不存在")
        CartLogic.delete_by_ids(_id)
        return ResponseUtil().success()
